//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Publisher.Wcf
{
    using System;
    using System.ServiceModel;
    using Microsoft.Research.eResearch.Blackboard.Common;

    /// <summary>
    /// Provides an extension that the WCF ServiceHost can expose to allow the <see cref="WcfPublisherMessageService"/>
    /// to fire events (from a WCF-created thread) when a new subscription profile is received.
    /// </summary>
    public class PublisherSubscriptionReceiverExtension : IExtension<ServiceHostBase>
    {
        /// <summary>
        /// Method to be executed whenever message is recieved.
        /// </summary>
        private Action<SubscriptionProfile> onPublisherSubscriptionChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="PublisherSubscriptionReceiverExtension"/> class.
        /// </summary>
        /// <param name="onPublisherSubscriptionChanged">The on publisher subscription changed.</param>
        public PublisherSubscriptionReceiverExtension(Action<SubscriptionProfile> onPublisherSubscriptionChanged)
        {
            if (onPublisherSubscriptionChanged == null)
            {
                throw new ArgumentNullException("onPublisherSubscriptionChanged");
            }

            this.onPublisherSubscriptionChanged = onPublisherSubscriptionChanged;
        } 

        /// <summary>
        /// Enables an extension object to find out when it has been aggregated. 
        /// Called when the extension is added to the 
        /// <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Attach(ServiceHostBase owner)
        {
        } 

        /// <summary>
        /// Enables an object to find out when it is no longer aggregated. Called when an extension
        /// is removed from the <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Detach(ServiceHostBase owner)
        {
        } 

        /// <summary>
        /// Changes the aggregate subscription profile of the publisher.
        /// </summary>
        /// <param name="profile">The new subscription profile.</param>
        public void Subscribe(SubscriptionProfile profile)
        {
            onPublisherSubscriptionChanged(profile);
        } 
    }
}
